#include "fanye.h"
#include "star/raystar.h"
using namespace cocos2d;
CCScene * FanyeScene::scene()
{

	CCScene *scene = NULL;
	do 
	{
		scene = CCScene::create();
		CC_BREAK_IF(!scene);
		FanyeScene *fanye = FanyeScene::create();
		
		scene->addChild(fanye);
	} while (0);
	return scene;
}
bool FanyeScene::init()
{
	bool ret_value = false;
	do 
	{
		epstar = RayStar::create();
		
		addChild(epstar);
		
		//epstar->StartRun(ccp(200,200),ccc3(0,255,0));
		
		/*current_page_ = 0;
		CCMenuItemImage *next_page = CCMenuItemImage::create("agin.png","agin.png",this,menu_selector(FanyeScene::nextPage));
		next_page->setPosition(ccp(300,-100));
		CCMenuItemImage *last_page = CCMenuItemImage::create("agin.png","agin.png",this,menu_selector(FanyeScene::lastPage));
		last_page->setPosition(ccp(300,100));
		CCMenu *menu = CCMenu::create(next_page,last_page,NULL);
		addChild(menu);
		char path[20]={0};
		for(int i = 0;i<6;i++)
		{
			sprintf(path,"fanye/%d.png",i+1);
			fanyepage[i] = CCSprite::create(path);
			
			if(i%2 == 0)
			{
				fanyepage[i]->setAnchorPoint(ccp(1,0.5));
			}
			else
			{
				fanyepage[i]->setAnchorPoint(ccp(0,0.5));
			}
			fanyepage[i]->setPosition(ccp(300,200));
			addChild(fanyepage[i],0);
			fanyepage[i]->setVisible(false);
		}
		
		fanyepage[0]->setVisible(true);
		fanyepage[1]->setVisible(true);*/
		ret_value = true;
	} while (0);
	return ret_value;
}
void FanyeScene::nextflipPage(CCSprite * page_first,CCSprite *page_second)
{
		CCActionInterval *scaleto_x_zero = CCScaleTo::create(0.5,0,1);
		CCActionInterval *scaleto1_x_normal = CCScaleTo::create(0.5,1,1);
		CCActionInterval *skewto_y = CCSkewTo::create(0.5, 0,20);
		CCActionInterval *skewto_normal = CCSkewTo::create(0.5, 0,0);
		CCSpawn * sp = CCSpawn::create(scaleto_x_zero,skewto_y);
		page_first->runAction(sp);
		
		page_second->setSkewY(-20);
		page_second->setScaleX(0);
		
		page_second->runAction(CCSequence::create(CCDelayTime::create(0.5),CCSpawn::create(scaleto1_x_normal,skewto_normal)));

}

void FanyeScene::lastflipPage(CCSprite * page_first,CCSprite *page_second)
{
	CCActionInterval *scaleto_x_zero = CCScaleTo::create(0.5,0,1);
	CCActionInterval *scaleto1_x_normal = CCScaleTo::create(0.5,1,1);
	CCActionInterval *skewto_y = CCSkewTo::create(0.5, 0,-20);
	CCActionInterval *skewto_normal = CCSkewTo::create(0.5, 0,0);
	CCSpawn * sp = CCSpawn::create(scaleto_x_zero,skewto_y);
	page_second->runAction(sp);

	page_first->setSkewY(20);
	page_first->setScaleX(0);

	page_first->runAction(CCSequence::create(CCDelayTime::create(0.5),CCSpawn::create(scaleto1_x_normal,skewto_normal)));

}

void FanyeScene::nextPage(CCObject * sender)
{
	if(current_page_>=4)
		return;
	fanyepage[current_page_]->setVisible(true);
	this->reorderChild(fanyepage[current_page_],0);
	fanyepage[(current_page_+3)]->setVisible(true);
	this->reorderChild(fanyepage[current_page_+3],0);
	
	fanyepage[current_page_+1]->setVisible(true);
	this->reorderChild(fanyepage[current_page_+1],1);
	fanyepage[current_page_+2]->setVisible(true);
	this->reorderChild(fanyepage[current_page_+2],1);
	
	nextflipPage(fanyepage[current_page_+1],fanyepage[current_page_+2]);
	current_page_ += 2;
}
void FanyeScene::lastPage(CCObject * sender)
{
	if(current_page_<2)
		return;
	fanyepage[current_page_+1]->setVisible(true);
	this->reorderChild(fanyepage[current_page_+1],0);
	fanyepage[current_page_]->setVisible(true);
	this->reorderChild(fanyepage[current_page_],1);
	fanyepage[current_page_-1]->setVisible(true);
	this->reorderChild(fanyepage[current_page_-1],1);
	fanyepage[current_page_-2]->setVisible(true);
	this->reorderChild(fanyepage[current_page_-2],0);
	lastflipPage(fanyepage[current_page_-1],fanyepage[current_page_]);
	current_page_-=2;
}
void FanyeScene::onEnter()
{
	CCLayer::onEnter();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
		this,0,false);
}
bool FanyeScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint point = CCDirector::sharedDirector()->convertToGL(
		pTouch->locationInView());
	epstar->StartRun(point,ccc3(255,255,0));
	return true;
}
void FanyeScene::onExit()
{
	CCLayer::onExit();
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}